
#include "stdafx.h"
#include "tvideoplayer.h"
#include "tmediasourcefilter.h"
#include "include/tevent.h"

TVideoPlayer::TVideoPlayer()
	 :m_sourceFilter(NULL)
{

}

TVideoPlayer::~TVideoPlayer()
{
	 m_player.Release();
}

HRESULT TVideoPlayer::SetMediaSource(TMediaSource *source, CMediaType *mediaType)
{
	HRESULT hr;
	m_player.Release();
	m_player.Create();
	m_player.AddFilter(&m_sourceFilter);

	m_sourceFilter.ClearStream();
	m_sourceFilter.UpdateStream(source, mediaType);
	hr = m_player.Render(m_sourceFilter.GetPin(0));
	if(SUCCEEDED(hr))
	{
		double duration = 0;
		m_player.GetDuration(&duration);
	//	emit DurationChanged(duration);
		m_duration = duration;
	}
	return hr;
}

HRESULT TVideoPlayer::SetDisplayWindow(HWND hwnd)
{
	HRESULT hr = m_player.SetDisplayWindow(hwnd);
	m_player.SetNotifyWindow(hwnd);
	return hr;
}

HRESULT TVideoPlayer::ResizeDisplayWindow(QRect rect)
{
	RECT winRect = {rect.left(), rect.top(), rect.right() - 1, rect.bottom() - 1};
	return m_player.ResizeVideoWindow(winRect);
}

HRESULT TVideoPlayer::Play()
{
	//if(0 == m_timer)
	//	m_timer = startTimer(1000);
	QueryPosition();

	HRESULT hr = m_player.Run();
	//if(SUCCEEDED(hr))
	//	emit PlayPause(true);
	return hr;
}

HRESULT TVideoPlayer::PauseResume()
{
	HRESULT hr = E_FAIL;
	if(m_player.IsPaused())
	{
		hr = m_player.Run();
		//if(SUCCEEDED(hr))
		//	emit PlayPause(true);
	}
	else
	{
		hr = m_player.Pause();
		//if(SUCCEEDED(hr))
		//	emit PlayPause(false);
	}
	return hr;
}

HRESULT TVideoPlayer::Stop() 
{ 
	HRESULT hr = m_player.Stop();
	m_player.Release();
	//emit PositionChanged(0);
	if(m_timer)
	{
		//killTimer(m_timer);
		//m_timer = 0;
	}
	OnPlayFinish();
	return hr;
}

HRESULT TVideoPlayer::VolumeUp() 
{
	return m_player.IncreaseVolume(5);
}

HRESULT TVideoPlayer::VolumeDown()
{
	return m_player.IncreaseVolume(-5);
}

HRESULT TVideoPlayer::Seek(int pos) 
{ 
	return m_player.SetCurrentPosition(pos);
}

HRESULT TVideoPlayer::ChangeVolume(int volume) 
{
	return m_player.ChangeAudioVolume(volume);
}

// bool TVideoPlayer::event(QEvent *e)
// {
// 	if(e->type() == QEvent::Timer && m_player.IsRunning())
// 	{
// 		QueryPosition();
// 		return true;
// 	}
// 	return _base::event(e);
// }

void TVideoPlayer::QueryPosition()
{
	double pos = 0;
	m_player.GetCurrentPosition(&pos);
	if(pos != m_currentPosition)
	{
	//	m_currentPosition = pos;
	//	emit PositionChanged(pos);
	}
	int volume = m_player.GetAudioVolume();
	if(volume != m_CurrentVolume)
	{
	//	m_CurrentVolume = volume;
	//	emit VolumeChanged(volume);
	}
}

HRESULT TVideoPlayer::FastForward()
{
	HRESULT hr = E_FAIL;
	double pos = 0;
	hr = m_player.GetCurrentPosition(&pos);
	if(SUCCEEDED(hr))
		hr = m_player.SetCurrentPosition(pos + 5); 
	return hr;
}

HRESULT TVideoPlayer::Rewind()
{
	HRESULT hr = E_FAIL;
	double pos = 0;
	hr = m_player.GetCurrentPosition(&pos);
	if(SUCCEEDED(hr))
		hr = m_player.SetCurrentPosition(pos - 5);
	return hr;
}

bool TVideoPlayer::IsPlaying()
{
	return m_player.IsRunning();
}

void TVideoPlayer::OnPlayFinish()
{
// 	emit PositionChanged(m_duration);
// 	m_player.Release();
// 	TAsyncEventDispatcher::GetInstance()->PostEvent(
// 		new TPlayStateEvent(TPlayStateEvent::PST_PlayFinish));
}


void TVideoPlayer::HandleMessage(MSG *msg)
{
	long eventCode = m_player.GetEventCode(msg->wParam, msg->lParam);
	switch (eventCode)
	{
	case EC_PAUSED:
		{
			double pos = 0;
			m_player.GetCurrentPosition(&pos);
			if(int(pos) == m_duration)
			{
				OnPlayFinish();
			}
			break;
		}
	case EC_COMPLETE:
		OnPlayFinish();
		break;
	}
}